<template>
  <div class="CartBottomBar">
    <div class="check-content">
      <cart-check-button
        class="check-button"
        :isChecked="isChecked"
        @click.native="checkClick"
      />
      <span>全选</span>
    </div>
    <div class="count">合计{{ totalPrice }}</div>
    <div class="calculate" @click="calculate">结算({{ checkLength }})</div>
  </div>
</template>

<script>
import CartCheckButton from "./CartCheckButton.vue";
import { mapGetters } from "vuex";
export default {
  name: "CartBottomBar",
  components: { CartCheckButton },
  computed: {
    ...mapGetters(["cartList"]),
    totalPrice() {
      return (
        "￥" +
        this.cartList
          .filter((item) => {
            return item.checked;
          })
          .reduce((preValue, item) => {
            return preValue + item.price * item.count;
          }, 0)
          .toFixed(2)
      );
    },
    checkLength() {
      return this.cartList.filter((item) => item.checked).length;
    },
    isChecked() {
      if (this.cartList.length === 0) return false;
      return !this.cartList.find((item) => !item.checked);
    },
  },
  methods: {
    checkClick() {
      if (this.isChecked) {
        //全部选中
        this.cartList.forEach((item) => (item.checked = false));
      } else {
        this.cartList.forEach((item) => (item.checked = true));
      }
    },
    calculate() {
      this.$toast.show("商品结算成功", 1500);
    },
  },
};
</script>

<style scoped>
.CartBottomBar {
  height: 44px;
  line-height: 44px;
  width: 100%;
  position: absolute;
  bottom: 49px;
  background-color: #eee;
  padding-left: 10px;
  display: flex;
}
.check-content {
  display: flex;
  margin-right: 20px;
}
span {
  margin-left: 5px;
}
.count {
  font-size: 14px;
}
.calculate {
  width: 80px;
  position: absolute;
  right: 0;
  background-color: red;
  text-align: center;
  color: #fff;
}
</style>